
from os import environ
environ['PYGAME_HIDE_SUPPORT_PROMPT'] = '1'

import pygame
import random
import os,time
import hashlib

def calculate_md5(file_path):
    try:
        hash_md5 = hashlib.md5()
        with open(file_path, "rb") as f:
            for chunk in iter(lambda: f.read(4096), b""):
                hash_md5.update(chunk)
        return hash_md5.hexdigest()
    except:
        return None

file_md5 = calculate_md5('key.bin')
author_flag = file_md5 in ['4a24be5cf48847da2c055a6c43f19d4a']



class Pillar:
    UP_S=None
    DOWN_S=None
    def __init__(self,surface,x):
        self.surface=surface
        s=random.randint(12,15)
        up=random.randint(20,76-s-20)
        down=random.randint(20,76-s-up) if (52-up<60) else random.randint(52-up,76-s-up)
        self.UP_S = pygame.Surface((50, up * 10))
        self.DOWN_S = pygame.Surface((50, down * 10))
        pygame.draw.rect(self.UP_S, (255, 0, 0),(0,0,50,up*10),0)
        pygame.draw.rect(self.DOWN_S, (255, 0, 0),(0,0,50,down* 10),0)
        self.up_rect = self.UP_S.get_rect()
        self.down_rect = self.DOWN_S.get_rect()

        self.up_rect.x = x
        self.up_rect.y = 0
        self.down_rect.x = x
        self.down_rect.y = 768 - self.DOWN_S.get_rect().height

    def draw(self,surface):
        surface.blit(self.UP_S,(self.up_rect.x,self.up_rect.y))
        surface.blit(self.DOWN_S,(self.down_rect.x,self.down_rect.y))

    def action(self,dis=-4):
        self.up_rect.x=self.up_rect.x+dis
        self.down_rect.x = self.up_rect.x
        self.draw(self.surface);

    def check(self,rect):
        return self.up_rect.colliderect(rect) or self.down_rect.colliderect(rect)

class Ball:
    rect=None
    R, COLOR = 30, (0, 255, 0)

    def __init__(self,surface,x,y):
        self.surface=surface
        self.x=x;self.y=y

    def draw(self,surface):
        #self.rect = pygame.draw.circle(surface,self.COLOR,(self.x,self.y),self.R,0)
        self.rect = pygame.draw.rect(surface, self.COLOR, (self.x, self.y, self.R, self.R), 0)

    def move(self,dis):
        self.x=self.x+dis if (50<self.x+dis<300) else self.x
        self.draw(self.surface)

    def action(self,dis=2):
        self.y=self.y+dis
        self.draw(self.surface)

class Map:
    index=0
    map_surface=None

    def  __init__(self,surface):
        self.map_surface1=pygame.Surface((1200,70))
        self.map_surface2=pygame.Surface((1200,70))
        self.map1_x=0
        self.map2_x = 1200
        self.surface=surface
        lx=0;ly=0
        for i in range(0,1300,100):
            pygame.draw.line(self.map_surface1,(0,0,255),(lx,ly),(i,ly),2)
            pygame.draw.line(self.map_surface1,(0,0,255), (i, ly), (i,40-ly), 2)
            ly = 40 - ly; lx = i
        lx = 0;ly = 0
        for i in range(0, 1300, 100):
            pygame.draw.line(self.map_surface2, (0, 0,255), (i, ly), (i, 40 - ly), 2)
            pygame.draw.line(self.map_surface2,(0,0,255), (lx, ly), (i, ly), 2)
            ly=40-ly;lx=i

    def draw(self,surface):
        surface.blit(self.map_surface1, (self.map1_x,10))
        surface.blit(self.map_surface1, (self.map1_x,720))
        surface.blit(self.map_surface2, (self.map2_x,10))
        surface.blit(self.map_surface2, (self.map2_x,720))

    def action(self,dis=-5):
        self.map1_x = self.map1_x + dis if (self.map1_x > -1200) else 1200
        self.map2_x = self.map2_x + dis if (self.map2_x > -1200 ) else 1200
        self.draw(self.surface)

def init():
    global ball,map,now,pause,start_ticks
    now = 0
    start_ticks = pygame.time.get_ticks()
    pause = False
    ball = Ball(screen, 100, 140)
    pillars.clear()
    pillars.append(Pillar(screen, 600))
    pillars.append(Pillar(screen, 900))
    pillars.append(Pillar(screen, 1200))
    pygame.time.Clock().tick(30)


current_file_path = os.path.abspath(__file__)
current_directory = os.path.dirname(current_file_path)
ball=None;pillars=[];map=None;
run_flag = True;now = 0;pause = False
pygame.init()
pygame.mixer.init()
#pygame.mixer.music.load(f'{current_directory}/fate.mp3')
#pygame.mixer.music.play(-1)
font_path = f"{current_directory}/wqy-microhei.ttc"
font = pygame.font.Font(font_path, 24)
SCREEN_SIZE = (1024, 768)
pygame.display.set_caption('躲避障碍游戏')
screen = pygame.display.set_mode(SCREEN_SIZE,0,32)
init()
pygame.display.update()




while run_flag:

    pygame.time.Clock().tick(30)
    now=(now+1)%(80)
    if (now==0 and not pause):pillars.append(Pillar(screen,1200))
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run_flag = False

        if event.type == pygame.KEYDOWN:
            if event.key==pygame.K_r:
                init()
            if event.key==pygame.K_SPACE or event.key==pygame.K_UP or event.key==pygame.K_i:
                if (not pause): ball.action(-40)
            if event.key == pygame.K_LEFT or event.key==pygame.K_j:
                if (not pause):ball.move(-50)
            if event.key == pygame.K_RIGHT or event.key==pygame.K_l:
                if (not pause):ball.move(50)
            if event.key == pygame.K_DOWN or event.key==pygame.K_k:
                if (not pause):ball.action(40)

    if (not pause):
        screen.fill((0, 0, 0))
        [p.action() for p in pillars]
        if (pillars[0].up_rect.x<0):pillars.remove(pillars[0])
        ball.action()
        pause = pillars[0].check(ball.rect)

        ticks = pygame.time.get_ticks()
        elapsed_time = int((ticks-start_ticks) / 1000)  # 将毫秒转换为秒
        if not author_flag and elapsed_time>=20: pause = True
        time_text = f"敲方向键或空格键可控制小球运动，敲 r 键重置。 时长： {elapsed_time} 秒  "  # 格式化时间文本
        text_surface = font.render(time_text, True, (255, 255, 255))
        screen.blit(text_surface, (10, 10))
    pygame.display.update()
pygame.quit()
